package com.md.basedpc;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 项目名称：com.yxt.phutils.common
 * 类名称：PHHighLightWordsUtils
 * 类描述：文本高亮显示,支持多个数组
 * 创建人：wuht
 * 创建时间：2019-12-05 15:14
 */
public class PHHighLightWordsUtils {


    /***
     * 指定关键字高亮 字符串整体高亮
     * @param originString    原字符串
     * @param keyWords        高亮字符串
     * @param highLightColor  高亮色值
     * @return 高亮后的字符串
     */
    public static SpannableString getHighLightWord(String originString, String keyWords, int highLightColor) {
        SpannableString originSpannableString = new SpannableString(originString);
        if (!TextUtils.isEmpty(keyWords)) {
            Pattern pattern = Pattern.compile(keyWords);
            Matcher matcher = pattern.matcher(originSpannableString);
            while (matcher.find()) {
                int startIndex = matcher.start();
                int endIndex = matcher.end();
                originSpannableString.setSpan(new ForegroundColorSpan(highLightColor), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return originSpannableString;
    }

    /***
     * 指定关键字高亮 支持分段高亮   测试1234测1234试(测试为高亮字,实现测试/测/试分别高亮)
     * @param originString
     * @param keyWords
     * @param highLightColor
     * @return
     */
    public static SpannableString getHighLightWords(String originString, String keyWords, int highLightColor) {
        SpannableString originSpannableString = new SpannableString(originString);
        if (!TextUtils.isEmpty(keyWords)) {
            for (int i = 0; i < keyWords.length(); i++) {
                Pattern p = Pattern.compile(String.valueOf(keyWords.charAt(i)));
                Matcher m = p.matcher(originSpannableString);
                while (m.find()) {
                    int start = m.start();
                    int end = m.end();
                    originSpannableString.setSpan(new ForegroundColorSpan(highLightColor), start, end,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return originSpannableString;
    }


    /***
     * 指定关键字数组高亮
     * @param originString 原字符串
     * @param keyWords   高亮字符串数组
     * @param highLightColor 高亮色值
     * @return 高亮后的字符串
     */
    public static SpannableString getHighLightWordsArray(String originString, String[] keyWords, int highLightColor) {
        SpannableString originSpannableString = new SpannableString(originString);
        if (keyWords != null && keyWords.length > 0) {
            for (int i = 0; i < keyWords.length; i++) {
                Pattern p = Pattern.compile(keyWords[i]);
                Matcher m = p.matcher(originSpannableString);
                while (m.find()) {
                    int start = m.start();
                    int end = m.end();
                    originSpannableString.setSpan(new ForegroundColorSpan(highLightColor), start, end,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return originSpannableString;
    }

    /***
     * 指定关键字数组高亮
     * @param originString 原字符串
     * @param keyWords   高亮字符串集合
     * @param highLightColor 高亮色值
     * @return 高亮后的字符串
     */
    public static SpannableString getHighLightWordsCollection(String originString, List<String> keyWords, int highLightColor) {
        SpannableString originSpannableString = new SpannableString(originString);
        if (keyWords != null && keyWords.size() > 0) {
            for (int i = 0; i < keyWords.size(); i++) {
                Pattern p = Pattern.compile(keyWords.get(i));
                Matcher m = p.matcher(originSpannableString);
                while (m.find()) {
                    int start = m.start();
                    int end = m.end();
                    originSpannableString.setSpan(new ForegroundColorSpan(highLightColor), start, end,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return originSpannableString;
    }

}
